package com.room2.ui;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.room2.R;
import com.room2.model.User;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment {

    private UserViewModel viewModel;

    public UserFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(getActivity()).get(UserViewModel.class);
        final TextView textView = getView().findViewById(R.id.tv_content);
        Button btn_add = getView().findViewById(R.id.btn_add);
        Button btn_updata = getView().findViewById(R.id.btn_updata);
        Button btn_del = getView().findViewById(R.id.btn_del);
        Button btn_clear = getView().findViewById(R.id.btn_clear);

        viewModel.getGetAllData().observe(getActivity(), new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                StringBuilder sb = new StringBuilder();
                for (User s : users) {
                    sb.append("id:").append(s.getId()).append("姓名:").append(s.getName())
                            .append("地址:").append(s.getAddress()).append("\n");
                }
                textView.setText(sb.toString());
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user1 = new User("aaa", 20, "AA");
                User user2 = new User("bbb", 14, "BB");
                viewModel.insert(user1, user2);
            }
        });

        btn_updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user1 = new User("更新111", 200, "更新AA");
                user1.setId(20);
                User user2 = new User("更新222", 140, "更新BB");
                user2.setId(19);
                viewModel.update(user1, user2);
            }
        });


        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.delete("aaa");
            }
        });


        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.clear();
            }
        });


    }
}
